const tvvsdk_constants = require('./tvvsdk_constants.js')
const tvConstants = tvvsdk_constants.constants
const tvTipsConstants = tvConstants.tipsConstants
const tvvssdk_core = require('./tvvssdk_core.js')
const tvvssdk_tools = require('./tvvsdk_tools.js')
let currentPlatform

/**
 * 初始化视频流参数配置
 * @param {Object} options 参数配置
 * @param {Object} callback 回调
 */
function init(options, callback) {
  tvvssdk_core.exeInit(options, callback)
}

function queryRoomList(callback) {
  tvvssdk_tools.exeQueryRoomList(callback)
}

function getOnlineUsers(callback) {
  tvvssdk_core.exeGetOnlineUsers(callback)
}

function createConnection(callbacks) {
  tvvssdk_core.exeCreateConnection(callbacks)
}

/**
 * 加入房间
 * @param {Object} roomId 房间号
 * @param {Object} name 用户名
 * @param {Object} callbacks 回调
 */
function joinRoom(sessionId, roomId, name, callbacks) {
  tvvssdk_core.exeJoinRoom(sessionId, roomId, name, callbacks)
  // if (!designer) {
  // 	designer = createCanvasDesigner()
  // }
  // designer.init();
}

function queryRoom(sessionId, roomId, callback) {
  tvvssdk_core.exeQueryRoom(sessionId, roomId, callback)
}

function queryDevice(sessionId, callback) {
  tvvssdk_core.exeQueryDevice(sessionId, callback)
}

function compositeRoom(sessionId, roomId, userList) {
  tvvssdk_core.exeCompositeRoom(sessionId, roomId, userList)
}

/**
 * 显示视频流
 * @param {Object} videoObj
 * @param {Object} stream
 */
function attachMediaStream(videoObj, stream) {
  // if (stream && videoObj)
  tvvssdk_core.exeAttachMediaStream(videoObj, stream)
}

/**
 * 退出房间
 */
function leaveRoom(sessionId) {
  tvvssdk_core.exeLeaveRoom(sessionId)
}

/**
 * 屏幕共享
 * @param {Object} share 开启 | 关闭
 * @param {Object} callback 回调
 */
function shareScreen(sessionId, enable, callback) {
  tvvssdk_core.exeShareScreen(sessionId, enable, callback)
}

function mike(sessionId, enable, callback) {
  tvvssdk_core.exeMike(sessionId, enable, callback)
}

/**
 * 开启或关闭视频流
 * @param {Object} enable true|false
 * @param {Object} callback 回调
 */
function publish(sessionId, enable, callback) {
  tvvssdk_core.exePublish(sessionId, enable, callback)
}

/**
 * ws 群发房间消息
 * @param {Integer} roomId 房间号
 * @param {Object} operation 操作
 * @param {Object} content 内容
 * @param {Object} callback 回调
 */
function sendRoomMessage(roomId, operation, content, callback) {
  const resp = {}
  if (!roomId) {
    resp.success = false
    resp.msg = tvTipsConstants.roomIdError
    return callback(resp)
  }
  tvvssdk_core.exeSendRoomMessage(roomId, operation, content, callback)
}

/**
 * 单发消息
 * @param {Object} sender 发送者
 * @param {Object} recver 接收者
 * @param {Object} content 内容
 * @param {Object} callback 回调
 */
function sendSingleData(sessionId, roomId, sender, recver, content, callback) {
  tvvssdk_core.exeSendData(sessionId, roomId, tvConstants.singleMessage, sender, recver, '', content, callback)
}

/**
 * 单发消息(带接收者对方平台信息)
 * @param {Object} sender 发送者
 * @param {Object} recver 接收者
 * @param {Object} platform 接收者平台
 * @param {Object} content 内容
 * @param {Object} callback 回调
 */
function sendSingleData2(sessionId, roomId, sender, recver, platform, content, callback) {
  tvvssdk_core.exeSendData(sessionId, roomId, tvConstants.singleMessage, sender, recver, platform, content, callback)
}

/**
 * 群发消息
 * @param {Object} sender 发送者
 * @param {Object} content 内容
 * @param {Object} callback 回调
 */
function sendGroupData(sessionId, roomId, sender, receiver, content, callback) {
  tvvssdk_core.exeSendData(sessionId, roomId, tvConstants.groupMessage, sender, receiver, currentPlatform, content, callback)
}

/**
 * 设置码率
 * @param {Object} bitrate 码率值  1024=1kb/s  1mbps = 1024x1024
 * @param {Object} callback
 */
function setBitrate(sessionId, bitrate, callback) {
  tvvssdk_core.exeSetBitrate(sessionId, bitrate, callback)
}

/**
 * 重设置会场分辨率和码率
 * @param confSize
 * @param confBr
 */
function reconfigConf(sessionId, roomId, confSize, confBr, confFps, callback) {
  tvvssdk_core.exeReconfigConf(sessionId, roomId, confSize, confBr, confFps, callback)
}

/**
 * 获取所有设备列表
 * @param callback
 */
function getAllDevices(callback) {
  tvvssdk_core.exeGetAllDevices(callback)
}

function getDeviceEnvironment(callback) {
  tvvssdk_tools.exeGetDeviceEnvironment(callback)
}

/**
 * 设置分辨率
 * @param {Object} resolution 分辨率,只支持360P,720P,1080P
 * @param {Object} callback
 */
function setResolution(sessionId, resolution, callback) {
  tvvssdk_core.exeSetResolution(sessionId, resolution, callback)
}

function getQos(sessionId, callback) {
  tvvssdk_core.exeGetQos(sessionId, callback)
}

function initQos() {
  tvvssdk_core.exeInitQos()
}

/**
 * 获取本地QOS信息
 * @param {Object} enable 开启或关闭
 * @param {Object} callback
 */
function startLocalQos(sessionId, callback) {
  tvvssdk_core.exeStartLocalQos(sessionId, callback)
}

function stopLocalQos() {
  tvvssdk_core.exeStopLocalQos()
}

/**
 * @param {Object} index 远端画面 从1开始
 * @param {Object} callback
 */
function startRemoteQos(index, callback) {
  tvvssdk_core.exeStartRemoteQos(index, callback)
}

function stopRemoteQos(index) {
  tvvssdk_core.exeStopRemoteQos(index)
}

// 获取PeerConnection对象
function getPeer(sessionId) {
  return tvvssdk_core.exeGetPeer(sessionId)
}

// 开始推流
function streamStart(sessionId, options, roomId, callbacks) {
  return tvvssdk_core.exeStreamStart(sessionId, options, roomId, callbacks)
}

// 关闭推流
function streamStop(sessionId) {
  return tvvssdk_core.exeStreamStop(sessionId)
}

function invite28181(sessionId, deviceId, callback) {
  return tvvssdk_core.exeInvite28181(sessionId, deviceId, callback)
}

function bye28181(sessionId, deviceId, callback) {
  return tvvssdk_core.exeBye28181(sessionId, deviceId, callback)
}

function queryLog(sessionId, session_id, session_name, room_name, importance, type, callback) {
  return tvvssdk_core.exeQueryLog(sessionId, session_id, session_name, room_name, importance, type, callback)
}

function truncateLog(sessionId, callback) {
  return tvvssdk_core.exeTruncateLog(sessionId, callback)
}

module.exports = {
  // 初始化参数,检查环境是否支持webrtc,设置服务器参数
  init: init,
  queryRoomList: queryRoomList,
  // login: login,
  // loginOut: loginOut,
  // 单对单发送消息
  // sendSingleMessage: sendSingleMessage,
  // 设置接收消息回调
  // addMessageEventListener: addMessageEventListener,
  // 获取在线成员
  getOnlineUsers: getOnlineUsers,
  // 会议群发消息
  sendRoomMessage: sendRoomMessage,
  // 创建连接
  createConnection: createConnection,
  // 加入房间
  joinRoom: joinRoom,
  // 查询房间
  queryRoom: queryRoom,
  // 查询28181摄像头
  queryDevice: queryDevice,
  // 编排房间显示顺序
  compositeRoom: compositeRoom,
  // 添加媒体流
  attachMediaStream: attachMediaStream,
  // 离开房间
  leaveRoom: leaveRoom,
  // 屏幕共享
  shareScreen: shareScreen,
  // 开关麦克风
  mike: mike,
  // 开关摄像头
  publish: publish,
  // 单发消息
  sendSingleData: sendSingleData,
  sendSingleData2: sendSingleData2,
  // 群发消息
  sendGroupData: sendGroupData,
  // 获取多媒体设备
  getAllDevices: getAllDevices,
  // 获取设备环境
  getDeviceEnvironment: getDeviceEnvironment,
  // 设置本地摄像头分辨率
  setResolution: setResolution,
  // 设置本地码率
  setBitrate: setBitrate,
  // 重配置会场参数
  reconfigConf: reconfigConf,
  // 获取QOS信息
  getQos: getQos,
  // 初始化Qos信息
  initQos: initQos,
  // 开始获取本地QOS
  startLocalQos: startLocalQos,
  // 结束获取本地QOS
  stopLocalQos: stopLocalQos,
  // 开始远端QOS
  startRemoteQos: startRemoteQos,
  // 开始远端QOS
  stopRemoteQos: stopRemoteQos,
  getPeer: getPeer,
  // 开始推流
  streamStart: streamStart,
  // 关闭推流
  streamStop: streamStop,
  // 28181摄像头开始推流
  invite28181: invite28181,
  // 28181摄像头停止推流
  bye28181: bye28181,

  // 日志模块接口
  queryLog: queryLog,
  truncateLog: truncateLog

}
